#include "encoderoptionmgr.h"
#include "encoderoption.h"

#include <QHash>

EncoderOptionMgr *_instance = NULL;

volatile int encoder_counter = 0;


EncoderOptionMgr * EncoderOptionMgr::instance() {
    return _instance;
}

EncoderOptionMgr::EncoderOptionMgr () {
    _instance = this;
}


EncoderOption * EncoderOptionMgr::createOption(QSettings * set,EncoderOption *copy) {
    QString id = QString::number(encoder_counter++);
    EncoderOption *ret = NULL;
    ret = new EncoderOption(*set,id);
    connect(ret,SIGNAL(destroyed(QObject *)),this,SLOT(onEncoderOptionDestroy(QObject*)));
    m_options.insert(id,ret);
    if (!copy) {
	return ret;
    }else{
	*ret = *copy;
	ret->setId(id);
    }

    return ret;
}

EncoderOption * EncoderOptionMgr::getOption(const QString & key) const {
    return m_options.value(key,NULL);
}


void EncoderOptionMgr::onEncoderOptionDestroy(QObject * obj){
    EncoderOption *op;

    op = qobject_cast<EncoderOption *>(obj);
    if (!op) return;

    m_options.remove(op->id());
}
